<?php
/**
 * Created by PhpStorm.
 * User: VJLau
 * Date: 2018/1/30
 * Time: 下午3:12
 */

load()->func('tpl');

$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ($operation == 'display') {
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $list = pdo_fetchall("SELECT insurance_price.*, insurance.name FROM "
        . tablename('bontrip_insurance_price') . " AS insurance_price LEFT JOIN " . tablename('bontrip_insurance')
        . " as insurance ON insurance_price.insurance_id = insurance.id WHERE uniacid = '{$uniacid}'"
        . " ORDER BY insurance_price.id DESC LIMIT "
        . ($pindex - 1) * $psize . ',' . $psize);
    $total = pdo_fetchcolumn("SELECT COUNT(*) FROM "
        . tablename('bontrip_insurance_price') . " AS insurance_price LEFT JOIN " . tablename('bontrip_insurance')
        . " as insurance ON insurance_price.insurance_id = insurance.id WHERE uniacid = '{$uniacid}'");
    $pager = pagination($total, $pindex, $psize);

} elseif ($operation == 'post') {
    $id = intval($_GPC['id']);
    if (checksubmit('submit')) {
        $data = array(
            'insurance_id' => $_GPC['insurance_id'],
            'start_days' => $_GPC['start_days'],
            'end_days' => $_GPC['end_days'],
            'adult_price' => $_GPC['adult_price'],
            'child_price' => $_GPC['child_price'],
        );
        if (!empty($id)) {
            pdo_update('bontrip_insurance_price', $data, array('id' => $id));
        } else {
            pdo_insert('bontrip_insurance_price', $data);
           }
        message('保存保险价格成功！', $this->createWebUrl('insuranceprice', array('op' => 'display')), 'success');
    }
    $insurances = pdo_fetchall("select * from " . tablename('bontrip_insurance') . " where uniacid={$uniacid} and deleted_at is null");

    $insurance_price = pdo_fetch("select * from " . tablename('bontrip_insurance_price') . " where id={$id}");


} elseif ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $insurance = pdo_fetch("SELECT id FROM " . tablename('bontrip_insurance_price')
        . " WHERE id={$id} AND uniacid={$uniacid} AND deleted_at is null");

    if (empty($insurance)) {
        message('抱歉，保险价格不存在或是已经被删除！', $this->createWebUrl('insuranceprice', array('op' => 'display')), 'error');
    }
    pdo_delete('ims_bontrip_insurance_price', array('id' => $id));

    message('保险价格删除成功！', $this->createWebUrl('insuranceprice', array('op' => 'display')), 'success');

} else {
    message('请求方式不存在');

}
include $this->template('insuranceprice', TEMPLATE_INCLUDEPATH, true);